package cn.blankchn.www.windows;

import cn.blankchn.www.entity.Part;
import cn.blankchn.www.util.ReadCsvFactory;
import cn.blankchn.www.util.WriteCsvWithoutHandleFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by apple on 2018/4/2.
 */
public class MyFrame extends JFrame {

    private JButton btn_start, btn_choose_data, btn_choose_result, btn_get_all_data_loc, btn_get_result_loc;
    private JFileChooser chooserData, chooserResult;
    private JPanel panel_tool, panel_choose_result;
    private JTextField txt_num;
    private JTextArea area;

    private ArrayList<String> list_path = new ArrayList<String>();
    private String resultFile = "";

    public MyFrame() throws HeadlessException {
        initTool();
        initArea();
    }

    private void initArea() {
        area = new JTextArea(20, 15);
        add(area, BorderLayout.SOUTH);
    }

    private void initTool() {
        panel_tool = new JPanel(new GridLayout(1, 5));
        btn_start = new JButton("开始");
        btn_choose_data = new JButton("添加数据文件");
        btn_choose_result = new JButton("选择结果文件");
        btn_get_all_data_loc = new JButton("查看所有数据文件");
        btn_get_result_loc = new JButton("查看结果文件");
        txt_num = new JTextField();

        chooserData = new JFileChooser();
        chooserResult = new JFileChooser();
        btn_choose_data.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chooserData.setCurrentDirectory(new File("."));
                int result = chooserData.showOpenDialog(MyFrame.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    String name = chooserData.getSelectedFile().getAbsolutePath();
                    list_path.add(name);
                    area.append("添加数据文件：" + name + "\n");
                    pack();
                }
            }
        });

        btn_choose_result.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chooserResult.setCurrentDirectory(new File("."));
                int result = chooserResult.showOpenDialog(MyFrame.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    String name = chooserResult.getSelectedFile().getAbsolutePath();
                    area.append("添加结果文件：" + name + "\n");
                    resultFile = name;
                    pack();
                }
            }
        });

        btn_start.addActionListener(new ActionListener() {
                                        @Override
                                        public void actionPerformed(ActionEvent e) {
                                            int num = 0;
                                            try{
                                                num = Integer.parseInt(txt_num.getText().toString());
                                            }catch (Exception e1){
                                                area.append("报错：必须填写数字形式的起始序号\n"+e1.getMessage()+"\n");
                                                return;
                                            }

                                            for(String path : list_path){
                                                ArrayList<Part> list = ReadCsvFactory.handle(path, 0);
                                                if (list != null) {

//                                                    ArrayList<HandleResult> list_result = HandleFactory.handle(list, 0);
//
//                                                    if (list_result != null) {
//                                                        for (HandleResult result : list_result) {
//                                                            try {
////                                                                WriteCsvFactory.write(result, resultFile, num++);
//                                                                WriteCsvWithoutHandleFactory.write(result, resultFile, num++);
//                                                            } catch (IOException e1) {
//                                                                e1.printStackTrace();
//                                                                area.append("报错："+e1.getMessage()+"\n");
//                                                            }
//                                                            area.append(result.toString()+"\n");
//
//                                                        }
//                                                    }
                                                    for(Part part : list){
                                                        try {
                                                            WriteCsvWithoutHandleFactory.write(part, resultFile, num++);
                                                        } catch (IOException e1) {
                                                            e1.printStackTrace();
                                                        }
                                                    }
                                                }
                                            }

                                            area.append("完成\n");
                                        }
                                    }
        );

        btn_get_all_data_loc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.append("所有数据文件为：\n");
                for(String path : list_path){
                    area.append(path+"\n");
                }
            }
        });

        btn_get_result_loc.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                area.append("结果文件为："+resultFile+"\n");
            }
        });

        panel_tool.add(btn_start);
        panel_tool.add(btn_choose_data);
        panel_tool.add(btn_choose_result);
        panel_tool.add(btn_get_all_data_loc);
        panel_tool.add(btn_get_result_loc);
        panel_tool.add(txt_num);

        add(panel_tool, BorderLayout.NORTH);
    }

}
